package dao;

import bean.Goods;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class GoodsDao {
   public  static boolean addGoods(Goods goods) throws SQLException {
       QueryRunner  runner=new QueryRunner(MyDadtSoutce.getDataSource());
       String sql="insert into goods(id,name,price,count) value(?,?,?,?)";
      int num= runner.update(sql,new Object[]{goods.getId(),goods.getName(),goods.getPrice(),goods.getCount()});
       if(num>0)
       return true;
       else
           return  false;
   }
   public  static  boolean delGoods(){
       return true;
   }
   public  static  boolean ediGoods(){
       return  true;
   }
   public  static Goods findById(String id) throws SQLException{
       QueryRunner runner=new QueryRunner(MyDadtSoutce.getDataSource());
       String sql="select * from goods when id=?";
       Goods goods= (Goods) runner.query(sql,new BeanHandler(Goods.class),new  Object[]{id});
       return goods;
   }
   public  static List<Goods> findByName(String name){
       QueryRunner runner=new QueryRunner(MyDadtSoutce.getDataSource());
       List<Goods> goodsList= (Goods) runner.query(sql,new BeanListHandler<>(Goods.class),new  Object[]{name});
       return goodsList;
   }
}
